using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace SimplePong
{
    class PlayerIA
    {

        Texture2D texture;
        Vector2 position;
        Rectangle rectangle;

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        

        public Rectangle Rectangle
        {
            get { return rectangle; }
            set { rectangle = value; }
        }

        public PlayerIA(int player)
        {
            texture = GameGlobals.content.Load<Texture2D>("Textures/player");

            if (player == 1)
            {
                //Left Player
                position = new Vector2(25, GameGlobals.graphicsManager.PreferredBackBufferHeight/2 - texture.Height/2);
            }
            else
            {
                //Ricght Player
                position = new Vector2(GameGlobals.graphicsManager.PreferredBackBufferWidth - texture.Width - 25, 
                    GameGlobals.graphicsManager.PreferredBackBufferHeight / 2 - texture.Height / 2);
            }

            rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
        }

        public void Update(Ball ball)
        {
            if (ball.Position.Y > position.Y)
            {
                position.Y += 2.5f;
            }
            if (ball.Position.Y < position.Y)
            {
                position.Y -= 2.5f;
            }

            if (position.Y <= 0)
            {
                position.Y = 0;
            }
            if (position.Y + texture.Height >= GameGlobals.graphicsManager.PreferredBackBufferHeight)
            {
                position.Y = GameGlobals.graphicsManager.PreferredBackBufferHeight - texture.Height;
            }

            UpdateRectangle();
        }

        public void Draw(SpriteBatch spritebatch)
        {
            spritebatch.Draw(texture, position, Color.White);
        }

        private void UpdateRectangle()
        {
            rectangle.X = (int)position.X;
            rectangle.Y = (int)position.Y;
        }

    }
}
